package com.sls.web.manager.recharge.service.impl;

import java.lang.reflect.Type;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.CardImportLog;
import com.sls.web.manager.recharge.querybean.CardImportLogQuery;
import com.sls.web.manager.recharge.service.CardImportLogService;
import com.sls.web.manager.socket.SuperService;

/**
 * @author sxf
 * 
 */
@Service("cardImportLogService")
public class CardImportLogServiceImpl extends SuperService implements
		CardImportLogService {
	private static Logger log = Logger
			.getLogger(CardImportLogServiceImpl.class);

	/**
	 * @param serviceCode
	 * @param sendObj
	 * @param userInfoVO
	 * @param classType
	 *            类型信息(如:String,为String.class)
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public <T> T callService(String serviceCode, Head head, Object sendObj,
			UserInfoVO userInfoVO, Object... classType) throws Exception {
		Gson gson = CommonUtil.gson();
		if (head == null) {
			head = new Head();
			head.setRequestId(UUID.randomUUID().toString());
			head.setServiceCode(serviceCode);
			head.setIp(userInfoVO.getCurrentIp());
			head.setUserId(userInfoVO.getUserId());
		}
		SocketDTO requestDTO = super.send(head, sendObj);
		if (!TransmissionCode.SUCCESS.getCode().equals(
				requestDTO.getHead().getResponseCode())) {
			throw new ManagerException(requestDTO.getHead()
					.getResponseMessage());
		}
		String body = requestDTO.getBody();
		T t = null;
		if (body != null) {
			if (classType != null && classType.length > 0) {
				if (classType[0] instanceof Class) {
					t = gson.fromJson(body, (Class<T>) classType[0]);
				} else if (classType[0] instanceof Type) {
					t = gson.fromJson(body, (Type) classType[0]);
				}
			}
		}
		return t;
	}

	public boolean insert(CardImportLog info, UserInfoVO userInfoVO)
			throws Exception {
		this.callService(Constants.INSERT_CARDIMPORTLOG, null, info, userInfoVO);
		return true;
	}

	public List<CardImportLog> queryList(CardImportLogQuery query,
			UserInfoVO userInfoVO) throws Exception {
		log.info("Enter CardImportLogService queryPage method...");
		List<CardImportLog> infoPage = callService(
				Constants.QUERY_CARDIMPORTLOG_LIST, null, query, userInfoVO,
				new TypeToken<List<CardImportLog>>() {
				}.getType());
		log.info("Leave CardImportLogService queryPage method...");
		return infoPage;
	}

	public Pagination<CardImportLog> queryPage(CardImportLogQuery query,
			UserInfoVO userInfoVO) throws Exception {
		log.info("Enter CardImportLogService queryPage method...");
		Pagination<CardImportLog> infoPage = callService(
				Constants.QUERY_CARDIMPORTLOG_PAGE, null, query, userInfoVO,
				new TypeToken<Pagination<CardImportLog>>() {
				}.getType());
		log.info("Leave CardImportLogService queryPage method...");
		return infoPage;
	}

	public CardImportLog queryOne(CardImportLogQuery query,
			UserInfoVO userInfoVO) throws Exception {
		log.info("Enter CardImportLogService queryPage method...");
		CardImportLog infoPage = callService(Constants.QUERY_CARDIMPORTLOG_ONE,
				null, query, userInfoVO, CardImportLog.class);
		log.info("Leave CardImportLogService queryPage method...");
		return infoPage;
	}

	public boolean update(CardImportLog info, UserInfoVO userInfoVO)
			throws Exception {
		this.callService(Constants.UPDATE_CARDIMPORTLOG, null, info, userInfoVO);
		return true;
	}

	public boolean delete(CardImportLog info, UserInfoVO userInfoVO)
			throws Exception {
		this.callService(Constants.DELETE_CARDIMPORTLOG, null, info, userInfoVO);
		return true;
	}
}
